package com.ecmcloud.flow.node.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import com.ecmcloud.common.constants.ProcessInstanceConstant;
import com.ecmcloud.common.dto.flow.Node;
import com.ecmcloud.common.dto.flow.NodeUser;
import com.ecmcloud.common.utils.JsonMapperUtils;
import com.ecmcloud.flow.node.AssignUserStrategy;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 发起人自选
 */
@Component
@Slf4j
public class AssignUserSelfSelectStrategyImpl implements InitializingBean, AssignUserStrategy {
	
    @Override
    public List<String> handle(Node node, NodeUser rootUser, Map<String, Object> variables) {
        List<String> assignList = Lists.newArrayList();
        Object variable = variables.get(StrUtil.format("{}_assignee_select", node.getId()));
        log.info("{}-发起人自选参数:{}", node.getNodeName(), variable);
        if (variable == null) {
            return assignList;
        }
        List<NodeUser> nodeUserDtos = JsonMapperUtils.getInstance().parseObject(JsonMapperUtils.toJsonString(variable), new TypeReference<List<NodeUser>>() {});
        List<String> collect = nodeUserDtos.stream().map(w -> String.valueOf(w.getId())).collect(Collectors.toList());
        assignList.addAll(collect);
        return assignList;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        afterPropertiesSet(ProcessInstanceConstant.AssignedTypeClass.SELF_SELECT);
    }
}
